#############################################################################
# Copyright (c) 2009 Dmitry Grushin <dgrushin@gmail.com>.
#
# This file is part of clupower (cluster power manager utility)
#
# Clupower is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# Clupower is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with Clupower.  If not, see <http://www.gnu.org/licenses/>.
#
# Contributors:
#    Dmitry Grushin <dgrushin@gmail.com> - initial API and implementation
#############################################################################

class CPError < Exception
  def initialize(*args)
    if args.empty?
      raise NotImplementedError.new("#{self.class.name}")
    else
      super(*args)
    end
  end

  def set_cause(error)
    @cause = error
    return self
  end

  def get_trace
    if @cause.respond_to?("get_trace")
      return message() + "\nCaused by: " + @cause.get_trace
    elsif @cause
      return message() + "\nCaused by: " + @cause.message
    else
      return message()
    end
  end
end

class CPInitError < CPError
  def initialize(*args)
    if args.empty?
      super "Initialization error"
    else
      super(*args)
    end
  end
end

class CPI1 < CPInitError
  def initialize(aname)
    super "Unknown power manager heuristics '#{aname}'"
  end
end

class CPI2 < CPInitError
  def initialize(name)
    super "Unknown wake up method '#{name}'"
  end
end

class CPI3 < CPInitError
  def initialize(name)
    super "Another instance of server is already running. If not, remove the lock file '#{name}'"
  end
end

class CPI4 < CPInitError
  def initialize
    super "Unable to find a power manager server. Is it running?"
  end
end

class CPI5 < CPInitError
  def initialize
    super "Permission denied. You must have the same UID as the power manager server process"
  end
end

class CPI6 < CPInitError
  def initialize(name)
    super "Unable to read authorization key. Lock file is invalid: #{name}"
  end
end

class CPI7 < CPInitError
  def initialize(files)
    super "Unable to find configuration file. Tried these: #{files.collect{|f| f + " "}}"
  end
end

class CPI8 < CPInitError
  def initialize(lfile)
    super "Lock file is not found: #{lfile}"
  end
end

class CPI9 < CPInitError
  def initialize(option)
    super "Required option is not found: #{option}"
  end
end

class CPRuntimeError < CPError
  def initialize(*args)
    if args.empty?
      super "Runtime error"
    else
      super(*args)
    end
  end
end

# Command execution failed with the message
class CPExecError < CPRuntimeError
  def initialize(message)
    super message
  end
end

class CPE2 < CPRuntimeError
  def initialize(node)
    super "Unable to get node '#{node}' ip address"
  end
end

class CPE3 < CPRuntimeError
  def initialize(node)
    super "Cannot find node '#{node}' MAC address"
  end
end

class CPE4 < CPRuntimeError
  def initialize(node)
    super "Unable to get node '#{node}' MAC address"
  end
end

class CPE5 < CPRuntimeError
  def initialize
    super "Error parsing list of nodes"
  end
end

class CPE6 < CPRuntimeError
  def initialize
    super "Error reading list of nodes from torque"
  end
end

class CPE7 < CPRuntimeError
  def initialize
    super "Error reading scheduler queue"
  end
end

class CPE8 < CPRuntimeError
  def initialize(node)
    super "Unable to switch node '#{node}' on"
  end
end

class CPE9 < CPRuntimeError
  def initialize(node)
    super "Error marking node '#{node}' ONLINE"
  end
end

class CPE11 < CPRuntimeError
  def initialize(node)
    super "Error putting node '#{node}' into sleep"
  end
end

class CPE12 < CPRuntimeError
  def initialize(node)
    super "Error marking node '#{node}' OFFLINE"
  end
end

class CPE13 < CPRuntimeError
  def initialize(node)
    super "Unable to wake up a node '#{node}'"
  end
end

class CPE14 < CPRuntimeError
  def initialize
    super "No nodes found on cluster. Unable to get PPN value"
  end
end

class CPE15 < CPRuntimeError
  def initialize(job_id)
    super "Error reading job status. Job id = #{job_id}"
  end
end

class CPE16 < CPRuntimeError
  def initialize(job_id)
    super "Error parsing job status xml output from qstat. Job id = #{job_id}"
  end
end

class CPE17 < CPRuntimeError
  def initialize(job_id)
    super "Error parsing output from checkjob command. Job id = #{job_id}"
  end
end

class CPE18 < CPRuntimeError
  def initialize(job_id, message="")
    super "Unable to release a job hold. Job id = #{job_id}. #{message}"
  end
end

class CPE20 < CPRuntimeError
  def initialize
    super "Error reading list of reservations from maui"
  end
end

class CPE21 < CPRuntimeError
  def initialize(node)
    super "Error executing ipmitool for node #{node}"
  end
end

class CPE22 < CPRuntimeError
  def initialize
    super "Timeout waiting for main server loop."
  end
end

class CPE23 < CPRuntimeError
  def initialize(node)
    super "Unable to get node #{node} uptime information."
  end
end

class CPInvalidState < Exception
end
